//
//  AppDelegate.swift
//  WeiBoShiBing
//
//  Created by Bingo on 15/10/26.
//  Copyright © 2015年 Bingo. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        LLSQliteManager.sharedManager
        
        setupAppearance()
        //手动创建window界面
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
//        let tabBar = LLMainController()
        //
//        window?.rootViewController = tabBar
//        window?.rootViewController = LLVistorView()
        
        window?.rootViewController = defaultController()
        
        window?.makeKeyAndVisible()
       
        return true
    }
    private func setupAppearance(){
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    
    }
    
    private func defaultController() ->UIViewController{
        
        if !LLUserAccount.userLogin(){
            
            return LLMainController()
        }
       
        return isNewVersion() ?LLNewFeatureViewController() :LLWelcomeViewController()
    
    }
    
    func switchRootController(isMain: Bool){
        window?.rootViewController = isMain ? LLMainController() :LLWelcomeViewController()
    }
    private func isNewVersion()->Bool{
        
        let versionString = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as!String
        let currentVersion = Double(versionString)!
        print("\(currentVersion)")
        
        let sanBoxVersionKey = "sanBoxVersionKey"
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sanBoxVersionKey)
        print("\(sandBoxVersion)")
        
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sanBoxVersionKey)
        
        
        return currentVersion > sandBoxVersion
    }
    
    func applicationDidEnterBackground(application: UIApplication) {
        
        LLStatusDAL.sharedInstance.clearCacheData()
    }
}

